package com.hua.syn;
//不安全的买票
//线程不安全

import org.omg.PortableServer.THREAD_POLICY_ID;

public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTickt station = new BuyTickt();

        new Thread(station,"苦逼的我").start();
        new Thread(station,"牛逼的你").start();
        new Thread(station,"可恶的黄牛").start();



    }
}





class BuyTickt implements Runnable{

    private int ticketNums = 10;
    boolean flag = true; //外部停止方式
    @Override
    public void run() {
        //买票
        while (flag){
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    //同步方法了  , 锁的是this
    private synchronized void buy() throws InterruptedException {
        //判断是否邮票
        if (ticketNums<=0){
            flag = false;
            return;
        }
        //模拟延时

        Thread.sleep(1000);


        System.out.println(Thread.currentThread().getName()+"买到了"+ticketNums);
        ticketNums--;
    }
}